﻿// -----------------------------------------------------------------------
// <copyright file="UnityConfig.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace NeosSDI.VsExtension
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class UnityConfig
    {
        internal static void AddUnityMappingForService(EnvDTE.Project web, string webPath, string serviceName)
        {
            string lineToAdd = "<register type=\"I" + serviceName + "\" mapTo=\"" + serviceName + "\" />";
            StringBuilder outputFile = new StringBuilder();
            string unityFileName = getUnityFileName(webPath);
            if (!string.IsNullOrEmpty(unityFileName))
            {
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader(unityFileName);
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains(lineToAdd))
                    {
                        file.Close();
                        return;
                    }
                    if (line.Contains("</container>"))
                    {
                        outputFile.AppendLine("\t\t\t\t" + lineToAdd);
                    }
                    outputFile.AppendLine(line);
                }
                file.Close();
                File.Delete(unityFileName);
                TextWriter tw = new StreamWriter(unityFileName);
                tw.WriteLine(outputFile.ToString());
                tw.Close();
            }
        }

        internal static void AddUnityMappingForRepository(EnvDTE.Project web, string webPath, string repositoryName)
        {
            StringBuilder outputFile = new StringBuilder();
            string unityFileName = getUnityFileName(webPath);
            if (!string.IsNullOrEmpty(unityFileName))
            {
                string line;
                System.IO.StreamReader file = new System.IO.StreamReader(unityFileName);
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("</container>"))
                    {
                        outputFile.AppendLine("\t\t\t\t<register type=\"I" + repositoryName + "\" mapTo=\"" + repositoryName + "\" />");
                    }
                    outputFile.AppendLine(line);
                }
                file.Close();
                File.Delete(unityFileName);
                TextWriter tw = new StreamWriter(unityFileName);
                tw.WriteLine(outputFile.ToString());
                tw.Close();
            }
        }

        private static string getUnityFileName(string webPath)
        {
            try
            {
                string webConfigFile = webPath + "\\web.config";
                string line;
                // Read the file and display it line by line.
                System.IO.StreamReader file = new System.IO.StreamReader(webConfigFile);
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Contains("unityConfigFile"))
                    {
                        int start = line.IndexOf("value=\"") + "value=\"".Length;
                        int end = line.IndexOf("\"", start + 1);
                        string unityFile = line.Substring(start, end - start);
                        string unityFilename = webPath + "\\" + unityFile;
                        if (unityFile != string.Empty && File.Exists(unityFilename))
                        {
                            file.Close();
                            return unityFilename;
                        }
                    }
                }

                file.Close();
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
